{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"AZURE_OPENAI_API_KEY\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agent_manager import AgentManager, BaseAgent\n",
    "\n",
    "manager = AgentManager()\n",
    "\n",
    "plumber_agent = BaseAgent(\n",
    "    type='plumber',\n",
    "    description='This is a plumber agent. You are responsible for talking to the plumber person. A plumber will send you messages. all communication that you will have will be with a plumber. ask the plumber what you need to ask on behalf of the main user. You can call the plumber at the user house and so on.',\n",
    ")\n",
    "\n",
    "dispatcher_agent = BaseAgent(\n",
    "    type='dispatcher',\n",
    "    description='This is a dispatcher agent. You are responsible for talking to the main user as a dispatcher. You must be aware of other agents and call them when needed. You are the only interface to the main user. all agents will use you to talk to the main user.',\n",
    ")\n",
    "\n",
    "manager.register_agent(plumber_agent)\n",
    "manager.register_agent(dispatcher_agent)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dispatcher_agent.activate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'agent_manager' from '/home/wjayesh/apps/multiagent/agent_manager.py'>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reload agent manager\n",
    "import importlib\n",
    "import agent_manager\n",
    "importlib.reload(agent_manager)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'type': 'function',\n",
       "  'function': {'name': 'ask_agent',\n",
       "   'description': \"Ask an agent of a given type a question. You are already given the list of agent types you can talk to. Determine what agent type would be best suited to answer a question and also what question should be asked. The agent types available to you are {'plumber': 'This is a plumber agent. You are responsible for talking to the plumber service.', 'dispatcher': 'This is a dispatcher agent. You are responsible for talking to the user as a dispatcher. You must be aware of other agents and call them when needed.'}. If you think you can answer the question yourself, DON'T ask another agent.\",\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'agent_type': {'type': 'string',\n",
       "      'description': 'The type of agent to ask the question to.'},\n",
       "     'question': {'type': 'string',\n",
       "      'description': 'The question to ask the agent.'}}}}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dispatcher_agent.tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_o5EcQB8rPNeKllqcqmU4FQYT', function=Function(arguments='{\"agent_type\":\"plumber\",\"question\":\"What is your name?\"}', name='ask_agent'), type='function')])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'I have asked the plumber for their name. You will get the information soon.'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dispatcher_agent.process_message(\"nice. also, what's the plumber's name?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'content': 'hi, my shower is broken. can you help me?', 'role': 'user'},\n",
       " {'content': 'I can help you get in touch with a plumber who can fix your shower. Let me contact a plumber for you.',\n",
       "  'role': 'assistant',\n",
       "  'function_call': None,\n",
       "  'tool_calls': [{'id': 'call_rIkXcqujjj4fObupmYGeEHR0',\n",
       "    'function': {'arguments': '{\"agent_type\":\"plumber\",\"question\":\"The user\\'s shower is broken. Can you assist with repairing it?\"}',\n",
       "     'name': 'ask_agent'},\n",
       "    'type': 'function'}]},\n",
       " {'tool_call_id': 'call_rIkXcqujjj4fObupmYGeEHR0',\n",
       "  'role': 'tool',\n",
       "  'name': 'ask_agent',\n",
       "  'content': \"I have put the question 'The user's shower is broken. Can you assist with repairing it?' in the queue for the agent of type plumber.You will hear back soon.\"},\n",
       " {'content': \"I've contacted a plumber to assist with repairing your broken shower. They will get back to us shortly.\",\n",
       "  'role': 'assistant',\n",
       "  'function_call': None,\n",
       "  'tool_calls': None}]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dispatcher_agent._session.messages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'content': None,\n",
       " 'role': 'assistant',\n",
       " 'function_call': None,\n",
       " 'tool_calls': [{'id': 'call_m0Hs5trlrFXkJEliGSIzUG4C',\n",
       "   'function': {'arguments': '{\\n  \"agent_type\": \"plumber\",\\n  \"question\": \"What could be the possible reasons my shower is not working?\"\\n}',\n",
       "    'name': 'ask_agent'},\n",
       "   'type': 'function'}]}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "json.loads(dispatcher_agent._session.messages[2].model_dump_json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['plumber: hi i am sidbing the plumber. how can i help?']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dispatcher_agent._queue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['dispatcher: The user lives at Schellingstr. 36 in Munich. Please arrange a visit to fix the broken toilet flush.']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plumber_agent._queue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletionMessage(content='Great! The plumber will arrive at Schellingstr. 36 in Munich in approximately 32 minutes to fix the broken toilet flush.\\n\\nIf you have any further questions or need additional assistance, please let me know.', role='assistant', function_call=None, tool_calls=None)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Great! The plumber will arrive at Schellingstr. 36 in Munich in approximately 32 minutes to fix the broken toilet flush.\\n\\nIf you have any further questions or need additional assistance, please let me know.'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plumber_agent.process_message(\"ok i will be there in 32 mins.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'type': 'function',\n",
       "  'function': {'name': 'ask_agent',\n",
       "   'description': \"Ask an agent of a given type a question. You are already given the list of agent types you can talk to. Determine what agent type would be best suited to answer a question and also what question should be asked. The agent types available to you are {'dispatcher': 'This is a dispatcher agent. You are responsible for talking to the user as a dispatcher. You must be aware of other agents and call them when needed. You are the only interface to the user. all agents will use you to talk to the user.'}. If you think you can answer the question yourself, DON'T ask another agent.\",\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'agent_type': {'type': 'string',\n",
       "      'description': 'The type of agent to ask the question to.'},\n",
       "     'question': {'type': 'string',\n",
       "      'description': 'The question to ask the agent.'}}}}}]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plumber_agent.tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "multiagent",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
